﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace umbraco
{
    using System;
    using System.Globalization;
    using System.Text;
    using umbraco.BusinessLogic.Actions;
    using umbraco.cms.businesslogic.member;
    using umbraco.cms.presentation.Trees;
    using umbraco.ACSExtensions;

    public class loadBusinessIdentityProviders : BaseTree
    {
        private readonly AcsManagementService acsManagementService;

        public loadBusinessIdentityProviders(string application) : base(application) 
        {
            this.acsManagementService = new AcsManagementService();
        }

        public override void RenderJS(ref StringBuilder Javascript)
        {
            Javascript.Append(
                @"
                function viewBusinessIdentityProviders() {
	                UmbClientMgr.contentFrame('members/ViewBusinessIdentityProviders.aspx');
                }
                
                function viewBusinessIdentityProviderMappings(identityProviderId, identityProviderName) {
	                UmbClientMgr.contentFrame('members/ViewBusinessIdentityProviderMappings.aspx?identityProviderId=' + identityProviderId + '&identityProviderName=' + identityProviderName);
                }
                
                function viewMemberGroupMappings(identityProviderId, identityProviderName, groupId, groupName) {
	                UmbClientMgr.contentFrame('members/ViewMemberGroupMappings.aspx?identityProviderId=' + identityProviderId + '&identityProviderName=' + identityProviderName + '&memberGroupId=' + groupId + '&memberGroupName=' + groupName);
                }

                function editMemberGroupMapping(mappingId) {
	                UmbClientMgr.contentFrame('members/EditMemberGroupMapping.aspx?mappingId=' + mappingId);
                }");
        }

        public override void Render(ref XmlTree tree)
        {
            string groupId = umbraco.library.Request("groupId");
            string groupName = umbraco.library.Request("groupName");
            string identityProviderId = umbraco.library.Request("identityProviderId");
            string identityProviderName = umbraco.library.Request("identityProviderName");

            if (string.IsNullOrEmpty(groupId))
            {
                if (string.IsNullOrEmpty(identityProviderId))
                {
                    // List business identity providers
                    var identityProviders = this.acsManagementService.RetrieveBusinessIdentityProviders();

                    foreach (var identityProvider in identityProviders)
                    {
                        var xmlNode = XmlTreeNode.Create(this);
                        xmlNode.Menu.Clear();
                        xmlNode.Menu.Add(ActionRefresh.Instance);
                        xmlNode.NodeID = identityProvider.Id.ToString();
                        xmlNode.Text = identityProvider.Name;
                        xmlNode.Source = this.GetTreeServiceUrl(identityProviderId) + "&identityProviderId=" + identityProvider.Id + "&identityProviderName=" + identityProvider.Name;
                        xmlNode.Action = "javascript:viewBusinessIdentityProviderMappings('" + identityProvider.Id + "', '" + identityProvider.Name + "');";
                        xmlNode.Icon = FolderIcon;
                        xmlNode.OpenIcon = FolderIcon;
                        xmlNode.HasChildren = true;

                        tree.Add(xmlNode);
                    }
                }
                else
                {
                    // List Member Groups
                    foreach (var group in MemberGroup.GetAll)
                    {
                        var xmlNode = XmlTreeNode.Create(this);
                        xmlNode.Menu.Clear();
                        xmlNode.Menu.Add(ActionNew.Instance);
                        xmlNode.Menu.Add(ContextMenuSeperator.Instance);
                        xmlNode.Menu.Add(ActionRefresh.Instance);
                        xmlNode.NodeID = string.Format(CultureInfo.InvariantCulture, "{0}|{1}|{2}|{3}", identityProviderId, identityProviderName, group.Id, group.Text);
                        xmlNode.Text = group.Text;
                        xmlNode.Icon = FolderIcon;
                        xmlNode.OpenIcon = FolderIcon;
                        xmlNode.HasChildren = true;
                        xmlNode.Source = this.GetTreeServiceUrl(identityProviderId) + "&identityProviderId=" + identityProviderId + "&identityProviderName=" + identityProviderName + "&groupId=" + group.Id + "&groupName=" + group.Text;
                        xmlNode.Action = "javascript:viewMemberGroupMappings('" + identityProviderId + "', '" + identityProviderName + "', '" + group.Id + "', '" + group.Text + "');";

                        tree.Add(xmlNode);
                    }
                }
            }
            else
            {
                // List Mappings rules
                var mappingRuleService = new MappingRuleService();
                var memberGroupId = int.Parse(groupId, CultureInfo.InvariantCulture);
                var mappings = mappingRuleService.RetrieveMappingsByMemberGroup(
                    new IdentityProviderInfo { Id = identityProviderId, Name = identityProviderName },
                    new MemberGroupInfo { Id = memberGroupId, Name = groupName });

                foreach (var mapping in mappings)
                {
                    if (mapping.Id != Guid.Empty)
                    {
                        var xmlNode = XmlTreeNode.Create(this);
                        xmlNode.Menu.Clear();
                        xmlNode.Menu.Add(ActionDelete.Instance);
                        xmlNode.NodeID = mapping.Id.ToString();
                        xmlNode.Text = mapping.Name;
                        xmlNode.Icon = "developerMacro.gif";
                        xmlNode.OpenIcon = "developerMacro.gif";
                        xmlNode.HasChildren = false;
                        xmlNode.Action = "javascript:editMemberGroupMapping('" + mapping.Id.ToString() + "');";

                        tree.Add(xmlNode);
                    }
                }
            }
        }

        protected override void CreateRootNode(ref XmlTreeNode rootNode)
        {
            rootNode.NodeType = "init" + this.TreeAlias;
            rootNode.NodeID = "init";
            rootNode.Menu.Clear();
            rootNode.Menu.Add(ActionRefresh.Instance);
            rootNode.Action = "javascript:viewBusinessIdentityProviders();";
        }
    }
}